//
//  NIDropDown.m
//  NIDropDown
//
//  Created by Bijesh N on 12/28/12.
//  Copyright (c) 2012 Nitor Infotech. All rights reserved.
//

#import "NIDropDown.h"
#import "QuartzCore/QuartzCore.h"

@interface NIDropDown ()
@property(nonatomic, strong) UITableView *table;
@property(nonatomic, strong) UIButton *btnSender;
@property(nonatomic, retain) NSArray *list;
@end

@implementation NIDropDown
@synthesize table;
@synthesize btnSender;
@synthesize list;
@synthesize delegate;

- (id)initDropDown:(UIButton *)b:(CGFloat *)height:(NSArray *)arr {
    btnSender = b;
    self = [super init];
    if (self) {
        // Initialization code
        CGRect btn = b.frame;
        
        CGFloat viewWidth = btn.size.width - 15;
        
        CGFloat viewX = btn.origin.x + 15;
        
        
        UIImageView *xiala = (UIImageView *)[b viewWithTag:22];
        self.frame = CGRectMake(viewX, btn.origin.y+btn.size.height, viewWidth, 0);
        self.list = [NSArray arrayWithArray:arr];
        
        table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, 0)];
        /*修改*/
        table.scrollEnabled = YES;
        table.bounces = NO;
        /**/
        table.delegate = self;
        table.dataSource = self;
        
        table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        table.separatorColor = [UIColor grayColor];
        [table setSeparatorInset:UIEdgeInsetsZero];
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.18];
        self.frame = CGRectMake(viewX, btn.origin.y+btn.size.height, viewWidth, *height);
        table.frame = CGRectMake(0, 0, viewWidth, *height);
        [xiala.layer setAffineTransform:CGAffineTransformMakeRotation(0.5f * 3.14159*2)];
        [UIView commitAnimations];
        
        
        [b.superview.superview addSubview:self];
        [self addSubview:table];
        UITableView *supertable = (UITableView *)[b.superview.superview viewWithTag:27];
        
        supertable.userInteractionEnabled = YES;
    }
    return self;
}

//------------ Sakura-2014-09-25 begin 为党员活动做的更改-------------
-(id)initDropDown:(UIButton *)b withFram:(CGRect)newFram withHeight:(CGFloat)dropHeight content:(NSArray *)arr forView:(UIView*)view{
    btnSender = b;
    self = [super init];
    if (self) {
        // Initialization code
        CGRect btn = b.frame;
        
        
        UIImageView *xiala = (UIImageView *)[b viewWithTag:22];
        self.frame = CGRectMake(newFram.origin.x, newFram.origin.y+newFram.size.height, newFram.size.width, 0);
        self.list = [NSArray arrayWithArray:arr];
        
        table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, newFram.size.width, 0)];
        /*修改*/
        table.scrollEnabled = YES;
        table.bounces = NO;
        /**/
        table.delegate = self;
        table.dataSource = self;
        
        table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        table.separatorColor = [UIColor grayColor];
        [table setSeparatorInset:UIEdgeInsetsZero];
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.18];
        self.frame = CGRectMake(newFram.origin.x, newFram.origin.y+newFram.size.height, newFram.size.width, dropHeight);
        table.frame = CGRectMake(0, 0, newFram.size.width, dropHeight);
        [xiala.layer setAffineTransform:CGAffineTransformMakeRotation(0.5f * 3.14159*2)];
        [UIView commitAnimations];
        
        
        [view addSubview:self];
        [self addSubview:table];
        
        UITableView *supertable = (UITableView *)[b.superview.superview viewWithTag:27];
        
        supertable.userInteractionEnabled = YES;
    }
    return self;
}
//------------ Sakura end ---------------

-(void)hideDropDown:(UIButton *)b {

    UIImageView *xiala = (UIImageView *)[b viewWithTag:22];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.18];
    self.frame = CGRectMake(215, 0, 105, 0);
    table.frame = CGRectMake(0, 0, 105, 0);
    [xiala.layer setAffineTransform:CGAffineTransformMakeRotation(0)];
    [UIView commitAnimations];
    UITableView *supertable = (UITableView *)[b.superview.superview viewWithTag:27];
    supertable.userInteractionEnabled = YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.list count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.font = [UIFont systemFontOfSize:12];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
    }
    cell.textLabel.text =[list objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selbg.png"]];
    cell.backgroundView = imageView;
    if ([cell.textLabel.text isEqualToString:btnSender.titleLabel.text]) {
        cell.backgroundColor = [UIColor colorWithRed:0.180 green:0.180 blue:0.180 alpha:1];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"select :%i",indexPath.row);
    
    [self hideDropDown:btnSender];
    UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
    [btnSender setTitle:c.textLabel.text forState:UIControlStateNormal];
    //代理的运行时安全判断
    if ([self.delegate respondsToSelector:@selector(niDropDownDelegateMethod:ForTitle:ForIndex:)]){
        [self.delegate niDropDownDelegateMethod:self ForTitle:list[indexPath.row] ForIndex:indexPath.row];
    }
    
}


@end
